"""AdminMenuSeedTableSeeder Seeder."""

from masoniteorm.seeds import Seeder
from app.models.AdminMenu import AdminMenu


class AdminMenuTableSeeder(Seeder):
    def run(self):
        """Run the database seeds."""
        print("清空 admin_menus 数据")
        print("写入 admin_menus 数据")
        AdminMenu.truncate()

        pid     = 0
        rank    = 0
        is_show = 1
        index   = {'title': 0, 'uri': 1, 'pid': 2, 'rank': 3, 'is_show': 4}
        root_menus = [
            ['系统设置', '#', pid, rank, is_show],
        ]
        menus = [
            ['菜单', 'admin/menu/index', '系统设置', rank, is_show],
            ['系统参数', 'admin/config/index', '系统设置', rank, is_show],
            ['管理员', 'admin/user/index', '系统设置', rank, is_show],
            ['角色', 'admin/role/index', '系统设置', rank, is_show],
            ['操作日志', 'admin/action-log/index', '系统设置', rank, is_show],
            ['登录日志', 'admin/user-login-log/index', '系统设置', rank, is_show],
            ['访问权限', 'admin/permission/index', '系统设置', rank, is_show],
        ]

        for menu in root_menus:
            admin_menu = AdminMenu.where('title', menu[index['title']]).first()
            if admin_menu is not None:
                print("{0}: {1} 已存在".format(menu[index['title']], menu[index['uri']]))
                continue

            print("写入 {0}: {1}".format(menu[index['title']], menu[index['uri']]))
            admin_menu2 = AdminMenu.where('title', menu[index['pid']]).first()
            pid         = 0
            if admin_menu2 is not None:
                pid = admin_menu2.id
            AdminMenu.generate_log(pid, menu[index['rank']], menu[index['title']], menu[index['uri']],
                                  menu[index['is_show']])

        for menu in menus:
            admin_menu = AdminMenu.where('title', menu[index['title']]).first()
            if admin_menu is not None:
                print("{0}:{1} 已存在".format(menu[index['title']], menu[index['uri']]))
                continue

            print("写入 {0}:{1}".format(menu[index['title']], menu[index['uri']]))
            admin_menu2 = AdminMenu.where('title', menu[index['pid']]).first()
            pid         = 0
            if admin_menu2 is not None:
                pid = admin_menu2.id
            AdminMenu.generate_log(pid, menu[index['rank']], menu[index['title']], menu[index['uri']],menu[index['is_show']])
